import express from "express"
import bodyParser from "body-parser";
import os from "os"
import main from "./openfaas_js/main.js";
import GLOBAL from "./openfaas_js/GLOBAL.js";

const {AUTH_riscv64, AUTH_x86, AUTH_x86swarm, AUTH_x86qemu} = GLOBAL

const URL_PREFIX_riscv64 = `http://admin:${AUTH_riscv64.PASSWORD}@${AUTH_riscv64.HOST}:${AUTH_riscv64.PORT}/`
const URL_PREFIX_x86 = `http://admin:${AUTH_x86.PASSWORD}@${AUTH_x86.HOST}:${AUTH_x86.PORT}/`
const URL_PREFIX_x86swarm = `http://admin:${AUTH_x86swarm.PASSWORD}@${AUTH_x86swarm.HOST}:${AUTH_x86swarm.PORT}/`
const URL_PREFIX_x86qemu = `http://admin:${AUTH_x86qemu.PASSWORD}@${AUTH_x86qemu.HOST}:${AUTH_x86qemu.PORT}/`
const ARCH = os.arch() === "riscv64" ? "riscv64" : "x86"
const URL_PREFIX = ARCH === "riscv64" ? URL_PREFIX_riscv64 : URL_PREFIX_x86qemu
const app = express()
app.use(bodyParser.json());
const port = 7280

app.get("/", (req, res) => {
    res.send("Hello World!")
})

app.post("/run", async (req, res) => {
    let body = req.body
    console.log(body)
    let {funcName, count} = body
    console.log(URL_PREFIX, `${ARCH}-${funcName}`, count)
    // await main.loopInvokeSingleFunction(URL_PREFIX, `${ARCH}-funcName`, count)
    res.send('POST request received');
})

app.listen(port, function () {
    console.log(`Chai_OpenFaaS listening at http://localhost:${port}`);
});